package com.atguigu.campusintelligentassistantsystem.service;

import com.atguigu.campusintelligentassistantsystem.entity.Canteen;
import com.atguigu.campusintelligentassistantsystem.entity.CanteenDish;
import com.atguigu.campusintelligentassistantsystem.entity.CanteenWindow;
import com.atguigu.campusintelligentassistantsystem.repository.CanteenDishRepository;
import com.atguigu.campusintelligentassistantsystem.repository.CanteenRepository;
import com.atguigu.campusintelligentassistantsystem.repository.CanteenWindowRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class CanteenService {

    @Autowired
    private CanteenRepository canteenRepository;

    @Autowired
    private CanteenWindowRepository canteenWindowRepository;

    @Autowired
    private CanteenDishRepository canteenDishRepository;

    /**
     * 获取所有食堂
     * @return 食堂列表
     */
    public List<Canteen> getAllCanteens() {
        return canteenRepository.findAll();
    }

    /**
     * 根据ID获取食堂
     * @param id 食堂ID
     * @return 食堂信息
     */
    public Canteen getCanteenById(Long id) {
        Optional<Canteen> canteen = canteenRepository.findById(id);
        return canteen.orElse(null);
    }

    /**
     * 添加新食堂
     * @param canteen 食堂信息
     * @return 保存后的食堂
     */
    public Canteen addCanteen(Canteen canteen) {
        return canteenRepository.save(canteen);
    }

    /**
     * 更新食堂信息
     * @param canteen 食堂信息
     * @return 更新后的食堂
     */
    public Canteen updateCanteen(Canteen canteen) {
        return canteenRepository.save(canteen);
    }

    /**
     * 删除食堂
     * @param id 食堂ID
     */
    public void deleteCanteen(Long id) {
        canteenRepository.deleteById(id);
    }

    /**
     * 根据食堂ID获取窗口列表
     * @param canteenId 食堂ID
     * @return 窗口列表
     */
    public List<CanteenWindow> getWindowsByCanteenId(Long canteenId) {
        return canteenWindowRepository.findByCanteenId(canteenId);
    }

    /**
     * 根据ID获取窗口
     * @param id 窗口ID
     * @return 窗口信息
     */
    public CanteenWindow getWindowById(Long id) {
        Optional<CanteenWindow> window = canteenWindowRepository.findById(id);
        return window.orElse(null);
    }

    /**
     * 添加新窗口
     * @param window 窗口信息
     * @return 保存后的窗口
     */
    public CanteenWindow addWindow(CanteenWindow window) {
        return canteenWindowRepository.save(window);
    }

    /**
     * 更新窗口信息
     * @param window 窗口信息
     * @return 更新后的窗口
     */
    public CanteenWindow updateWindow(CanteenWindow window) {
        return canteenWindowRepository.save(window);
    }

    /**
     * 删除窗口
     * @param id 窗口ID
     */
    public void deleteWindow(Long id) {
        canteenWindowRepository.deleteById(id);
    }

    /**
     * 根据窗口ID获取菜品列表
     * @param windowId 窗口ID
     * @return 菜品列表
     */
    public List<CanteenDish> getDishesByWindowId(Long windowId) {
        return canteenDishRepository.findByWindowId(windowId);
    }

    /**
     * 根据ID获取菜品
     * @param id 菜品ID
     * @return 菜品信息
     */
    public CanteenDish getDishById(Long id) {
        Optional<CanteenDish> dish = canteenDishRepository.findById(id);
        return dish.orElse(null);
    }

    /**
     * 添加新菜品
     * @param dish 菜品信息
     * @return 保存后的菜品
     */
    public CanteenDish addDish(CanteenDish dish) {
        return canteenDishRepository.save(dish);
    }

    /**
     * 更新菜品信息
     * @param dish 菜品信息
     * @return 更新后的菜品
     */
    public CanteenDish updateDish(CanteenDish dish) {
        return canteenDishRepository.save(dish);
    }

    /**
     * 删除菜品
     * @param id 菜品ID
     */
    public void deleteDish(Long id) {
        canteenDishRepository.deleteById(id);
    }
}